<template>
	<view class="content">

		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">我的设备</block>
		</cu-custom>

		<view class="cu-list menu sm-border">
			
			<view @click="addDevice" class="cu-item margin-top margin-bottom">
				<view class="content">
					<text class="text-blue"></text>
					<text class="text-grey">添加设备</text>
				</view>
				<view class="action">
					<image mode="widthFix" class="icon-add" src="../../static/icon_add.png"></image>
				</view>
			</view>
			
			<view v-for="(item,index) in deviceList" :key="index" class="cu-item" @click="itemOnClick(item.deviceno)">
				<view class="content padding-tb-sm">
					<view class="title">
						<view class="margin-right-xs">
							<text class="cuIcon-locationfill text-green"></text>
							<text class="text-blue margin-right-xs"></text>
							{{item.addrname}}
						</view>
						<view class="action">
							<button @click.stop="deleteClick(item.id)" class="cu-delete bg-white text-gray"><text class="cuIcon-delete"></text></button>
						</view>
					</view>
						
					<view class="text-gray text-sm">
						<view :class="item.online=='Y'?'bg-green':'bg-red'" class="cu-tag round light margin-right-xs">{{item.online=='Y'?'在线':'不在线'}}</view>
					</view>
				</view>
			</view>
			
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				deviceList: []
			}
		},
		onShow() {
			this.getDeviceList()
		},
		methods: {
			getDeviceList() {
				// var this
				uni.request({
					url: this.$constant.BASEURL + this.$constant.GET_DEVICE_LIST,
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						username: getApp().globalData.username,
						token: getApp().globalData.token,
						orgcode: getApp().globalData.orgcode
					},
					success: (res) => {
						var result = res.data
						this.deviceList = result.data
						console.log(this.deviceList)
					},
					complete() {

					}
				})
			},
			itemOnClick(deviceno) {
				uni.navigateTo({
					url: 'setting?deviceId=' + deviceno
				})
			},
			addDevice(){
				uni.navigateTo({
					url: 'increase'
				})
			},
			deleteClick(deviceno){
				let that = this
				console.log(deviceno)
				if (deviceno=='') {
					uni.showToast({
						title:'设备没有id，无法删除',
						icon:'none'
					})
					return
				}
				uni.showModal({
					title:'提示',
					content:'确定要删除这台设备吗?',
					success:function(res){
						if(res.confirm){
							that.deleteDevice(deviceno)
						}
					}
				})
			},
			deleteDevice(id){
				uni.request({
					url: this.$constant.BASEURL + this.$constant.DELETE_DEVICE,
					method: 'POST',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						username: getApp().globalData.username,
						token: getApp().globalData.token,
						id: id
					},
					success: (res) => {
						console.log(res)
						uni.showToast({
							title:res.data.message,
							icon:'none'
						})
						if(res.data.code == 0){
							this.getDeviceList()
						}
					},
					complete() {
				
					}
				})
			}
		}
	}
</script>

<style>
	.icon-add {
		width: 50upx;
		height: 50upx;
	}
	.title {
		display: flex;
		justify-content: space-between;
	}
	.cu-delete {
		width: 56upx;
		height: 56upx;
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>
